package com.example.labSystem.controller;

import com.example.labSystem.entity.Leave;
import com.example.labSystem.service.ILeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;

import java.util.List;

/**
 * <p>
 * 请假申请表 前端控制器
 * </p>
 *
 * @author 10
 * @since 2024-12-12
 */
@RestController
@RequestMapping("/leave")
public class LeaveController {

    @Autowired
    private ILeaveService leaveService;

    /**
     * 获取所有请假记录
     */
    @GetMapping("/list/all")
    public ResponseEntity<List<Leave>> getAllLeaves() {
        List<Leave> leaves = leaveService.getAllLeaves();
        return leaves.isEmpty() ?
                ResponseEntity.status(HttpStatus.NOT_FOUND).build() :
                ResponseEntity.ok(leaves);
    }

    /**
     * 获取用户的请假记录
     */
    @GetMapping("/list/{userID}")
    public ResponseEntity<List<Leave>> getLeavesByUserId(@PathVariable("userID") String userID) {
        List<Leave> leaves = leaveService.getLeavesByUserId(userID);
        return leaves.isEmpty() ?
                ResponseEntity.status(HttpStatus.NOT_FOUND).build() :
                ResponseEntity.ok(leaves);
    }


    /**
     * 提交请假申请
     */
    @PostMapping("/add")
    public ResponseEntity<String> submitLeaveRequest(@RequestBody Leave leave) {
        boolean isSubmitted = leaveService.submitLeaveRequest(leave);
        return isSubmitted ?
                ResponseEntity.status(HttpStatus.CREATED).body("请假申请提交成功") :
                ResponseEntity.status(HttpStatus.BAD_REQUEST).body("请假申请提交失败");
    }

    /**
     * 根据请假申请ID获取请假记录
     */
    @GetMapping("/{requestID}")
    public ResponseEntity<Leave> getLeaveByRequestID(@PathVariable("requestID") String requestID) {
        Leave leave = leaveService.getLeaveByRequestID(requestID);
        return leave != null ?
                ResponseEntity.ok(leave) :
                ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }

    /**
     * 更新请假记录
     */
    @PutMapping("/update")
    public ResponseEntity<String> updateLeave(@RequestBody Leave leave) {
        boolean isUpdated = leaveService.updateLeave(leave);
        return isUpdated ?
                ResponseEntity.ok("请假记录更新成功") :
                ResponseEntity.status(HttpStatus.BAD_REQUEST).body("请假记录更新失败");
    }

    /**
     * 审核请假记录
     */
    @PutMapping("/approve/{requestID}")
    public ResponseEntity<String> approveLeave(@PathVariable("requestID") String requestID,
                                               @RequestParam("status") String status) {
        boolean isApproved = leaveService.approveLeave(requestID, status);
        return isApproved ?
                ResponseEntity.ok("请假记录审核成功") :
                ResponseEntity.status(HttpStatus.BAD_REQUEST).body("请假记录审核失败");
    }

    /**
     * 删除请假记录
     */
    @DeleteMapping("/delete/{requestID}")
    public ResponseEntity<String> deleteLeave(@PathVariable("requestID") String requestID) {
        boolean isDeleted = leaveService.deleteLeave(requestID);
        return isDeleted ?
                ResponseEntity.ok("请假记录删除成功") :
                ResponseEntity.status(HttpStatus.BAD_REQUEST).body("请假记录删除失败");
    }
}
